#pragma once
#include <functional>
#include <map>

#include "Singleton.h"
#include "const.h"
#include <boost/json.hpp>

class HttpConnection;

using HttpHandler = std::function<void(std::shared_ptr<HttpConnection>)>;

class LogicSystem : public Singleton<LogicSystem> {
    friend class Singleton<LogicSystem>;

public:
    ~LogicSystem() = default;

    bool HandleHttp(http::verb method, const std::string& path, const std::shared_ptr<HttpConnection>& connection);

    void RegisterGet(const std::string& url, const HttpHandler& handler);

    void RegisterPost(const std::string& url, const HttpHandler& handler);

    LogicSystem(const LogicSystem&) = delete;

    LogicSystem& operator=(const LogicSystem&) = delete;

private:
    LogicSystem();

    static bool CheckJWT(const std::shared_ptr<HttpConnection>& connection);

    static std::string ResultError(int code, const std::string& msg);

    std::map<std::string, HttpHandler> _post_handlers;
    std::map<std::string, HttpHandler> _get_handlers;
};
